package com.caiyi.financial.nirvana.sms.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 加载配置文件属性
 *
 * @author lvchaohua
 */
public class PropertiesUtil {

    private PropertiesUtil() {
    }

    private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

    private static final String[] FILES = new String[]{"/application.properties"};
    private static final Map<String, Object> PROPERTIES = new HashMap<>();

    static {
        InputStream is = null;
        try {
            for (int i = 0; i < FILES.length; i++) {
                URL url = PropertiesUtil.class.getResource(FILES[i]);
                if (url == null) {
                    continue;
                }
                is = url.openStream();
                Properties pro = new Properties();
                pro.load(is);
                Enumeration<?> e = pro.propertyNames();
                while (e.hasMoreElements()) {
                    String key = e.nextElement().toString();
                    PROPERTIES.put(key, pro.get(key));
                }
            }
        } catch (Exception e) {
            logger.error("配置文件加载失败", e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    logger.error("配置文件加载失败", e);
                }
            }
        }
    }

    public static Object get(String key) {
        return PROPERTIES.get(key);
    }

    public static String getString(String key) {
        Object o = get(key);
        if (o == null)
            return "";
        else
            return o.toString();
    }

    public static int getInt(String key) {
        String s = getString(key);
        if (s == null || "".equals(s))
            return 0;
        else
            return Integer.parseInt(s);
    }

    public static long getLong(String key) {
        String s = getString(key);
        if (s == null || "".equals(s))
            return 0;
        else
            return Long.parseLong(s);
    }

    public static boolean getBoolean(String key) {
        String s = getString(key);
        if (s == null || "".equals(s))
            return false;
        else
            return Boolean.parseBoolean(s);
    }

}
